 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHARED;
using MySql.Data.MySqlClient;
using System.Xml.Linq;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            IDb msSql = new MYSQL("localhost", "patientp_main", "root", "");

            Element rootEl = new Element("Results");
            MasterElement rootMasterElement = new MasterElement(0);
            rootMasterElement.elements.Add(rootEl);

            //master element -> level 1
            Element Name = new Element("Name", true);
            Element Name2 = new Element("Name");
            MasterElement Systems = new MasterElement(1);
            Systems.AddElement(Name); 
            Systems.AddElement(Name2);
            Systems.Parent = rootMasterElement;

            //   //master element -> level 2
            Element address = new Element("Address", true);
            Element street = new Element("Street");
            Element city = new Element("City");
            Element postal = new Element("Postal");
            MasterElement sysdetails = new MasterElement(2);
            sysdetails.AddElement(address);
            sysdetails.AddElement(street);
            sysdetails.AddElement(city);
            sysdetails.AddElement(postal);
            sysdetails.Parent = Systems;

            MasterElementList list = new MasterElementList(rootMasterElement);
            list.Add(Systems);
            list.Add(sysdetails);

            MySqlCommand com = new MySqlCommand();
            
            com.CommandText = "Select Name, Street, City, Postal FROM mytable";

            BuildTree mani = new BuildTree();
            MasterElementList result = mani.BuildTreeFromDatabase(msSql, com, list, rootMasterElement);
            bool escape = false;
            //writeDTD(result);
            

            XElement doc = mani.GenerateXMLUsingGroupingTechnique(result, 2);
            
            using (TextWriter writer = File.CreateText("results"))
            {
                writeDTD(result, writer);
                doc.Save(writer);
	        }
            //doc.Save("RESULTS");

            
            
        }

        public static void writeXSD(MasterElementList input, TextWriter inputwriter)
        {
            String outstring = "";
            int level = 1;
            inputwriter.WriteLine("<schema>");
            for (int i = 0; i < input.Count; i++)
            {
                if (input[i].level < level)
                    break;
                else
                    level = input[i].level;

                for (int p = 0; p < input[i].elements.Count; p++)
                {
                    if (input[i].elements[p].isTagOnly == true)
                    {
                        inputwriter.WriteLine("<element name = \"" + input[i].elements[p].tagName.ToString() + "\">");
                        inputwriter.WriteLine("<complexType name=\"" + input[i].elements[p].tagName.ToString() + "\">");
                    }
                    else
                    {
                        if (input[i].elements.Count > 2 && p == 1)
                            inputwriter.WriteLine("<sequence>");
                        inputwriter.WriteLine("<element name = \"" + input[i].elements[p].tagName.ToString() + "\" type = \"" + input[i].elements[p].tagType.ToString() + "\"" +
                            "length = \"" + input[i].elements[p].columnSize.ToString() + "\"/>");

                        inputwriter.WriteLine("<attribute name=\"Type\" type=\"string\"/>");
                        inputwriter.WriteLine("<attribute name=\"Length\" type=\"string\"/>");
                        inputwriter.WriteLine("<attribute name=\"table\" type=\"string\"/>");
                        inputwriter.WriteLine("<attribute name=\"BaseColumnName\" type=\"string\"/>");
                    }


                }
                if (input[i].elements.Count > 2)
                {
                    inputwriter.WriteLine("</sequence>");
                    inputwriter.WriteLine("</complexType>");
                    inputwriter.WriteLine("</element>");
                }

            }
            inputwriter.WriteLine("</schema>");
        }

        public static void writeDTD(MasterElementList input, TextWriter inputwriter)
        {
            int level = 1;
                        //inputwriter.WriteLine("<?xml version=\"1.0\"?>");
            //inputwriter.WriteLine("<!DOCTYPE Results [");
            for (int i = 0; i < input.Count; i++)
            {
                if (input[i].level < level)
                    break;
                else
                    level = input[i].level;
                String output = "";
                for (int p = 0; p < input[i].elements.Count; p++)
                {

                    if (input[i].elements[p].isTagOnly == true)
                    {
                        output += "<!ELEMENT " + input[i].elements[p].tagName.ToString() + " (";
                    }
                    else
                    {
                        output += input[i].elements[p].tagName.ToString();
                        if (p != input[i].elements.Count - 1)
                            output += ",";
                    }


                }
                output += ")>";
                //inputwriter.WriteLine(output);

            }
            level = 1;
            for (int i = 0; i < input.Count; i++)
            {
                if (input[i].level < level)
                    break;
                else
                    level = input[i].level;

                for (int p = 0; p < input[i].elements.Count; p++)
                {

                    if (input[i].elements[p].isTagOnly != true)
                    {
                        //inputwriter.WriteLine("<!ELEMENT " + input[i].elements[p].tagName.ToString() + " (#PCDATE)>");
                        output += "<!ELEMENT " + input[i].elements[p].tagName.ToString() + " (#PCDATA)>";
                    }


                }

            }
            //inputwriter.WriteLine("]>");
            output = @ + output;
            
            XDocument doc = new XDocument(
                new XProcessingInstruction(target, data),
                new XDocumentType("Results", null, null, output),
                new XElement("Pubs", 
                    new XElement("Book",
                        new XElement("Title", "Artifacts of Roman Civilization"),
                        new XElement("Author", "Moreno, Jordao")
                    ),
                    new XElement("Book",
                        new XElement("Title", "Midieval Tools and Implements"),
                        new XElement("Author", "Gazit, Inbar")
                    )
                ),
                new XComment("This is another comment.")
            );
                    }
    }
}
